//
//  SliderToolBar.swift
//  NetMusic
//
//  Created by PushMeBaby on 16/2/2.
//  Copyright © 2016年 CoderC. All rights reserved.
//

import UIKit

class SliderToolBar: UIView {

    
    var statueButtonArray = NSMutableArray()
    let buttonCount = 4
    let buttonTitle = ["个性推荐", "歌单", "主播电台", "排行榜"]
    let sliderView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        backgroundColor = UIColor.whiteColor()
        
        for i in 0..<buttonCount {
            
            let button = UIButton()
            
            button.setTitle(buttonTitle[i], forState: UIControlState.Normal)
            button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
            button.tag = i
            button.titleLabel?.textColor = UIColor.blackColor()
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            addSubview(button)
            
            statueButtonArray.addObject(button)
            
            
        }
        
        sliderView.backgroundColor = UIColor.blackColor()
        addSubview(sliderView)
        
    }

    func buttonClick(button: UIButton) {
        
        let offSetX = OSScreenW / 4 * CGFloat( button.tag )
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
            
            self.sliderView.transform = CGAffineTransformMakeTranslation(offSetX, 0)
            
            }) { (anima) -> Void in
                return true
        }
        
        
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let buttonW = OSScreenW / CGFloat( buttonCount )
        var buttonX = buttonW
        
        for i in 0..<buttonCount {
            
            buttonX = CGFloat( i ) * buttonW
            

            
            let button = statueButtonArray[i] as! UIButton
            
            button.snp_makeConstraints(closure: { (make) -> Void in
                make.centerY.equalTo(self)
                make.height.equalTo(self)
                make.width.equalTo(buttonW)
                make.left.equalTo(buttonX)
                
            })
        }
        
        let button = statueButtonArray[0] as! UIButton
        
        
        
        sliderView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(button)
            make.width.equalTo(buttonW)
            make.centerY.equalTo(self.snp_bottom)
            make.height.equalTo(2)
        }
        
        
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
